//
//  ChatMessageCellFunctions.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/1.
//

import Foundation
import UIKit

//class JJBubbleView : UIImageView {
//    override var canBecomeFirstResponder: Bool{
//        return true
//    }
//}

func messageCell_createBubbleView(direction:EMMessageDirection,superView:UIView,aboveView:UIView,bubbleAdapter:MsgCellBubbleLayoutAdapter) -> UIImageView {
    //加气泡
    let bubbleView = UIImageView.init()
    bubbleView.backgroundColor = UIColor.orange
    superView.insertSubview(bubbleView, belowSubview: aboveView)
    let bubbleAdapter = MsgCellBubbleLayoutAdapterConfig.share.catAdapter
    bubbleView.image = bubbleAdapter.bubbleImage(direction: direction)
    
    let convertEdgeInsets = convertToEdgeInsets_direction(direction: direction, top: bubbleAdapter.resizableTop, bottom: bubbleAdapter.resizableBottom, fromSide: bubbleAdapter.resizableFromSide, toSide: bubbleAdapter.resizableToSide)
    
    bubbleView.snp.makeConstraints { make in
        make.top.equalTo(aboveView.snp.top).offset(CGFloat(-convertEdgeInsets.top))
        make.left.equalTo(aboveView.snp.left).offset(CGFloat(-convertEdgeInsets.left))
        make.bottom.equalTo(aboveView.snp.bottom).offset(CGFloat(convertEdgeInsets.bottom))
        make.right.equalTo(aboveView.snp.right).offset(CGFloat(convertEdgeInsets.right))
    }
    return bubbleView
}




let MESSAGECELL_IMAGE_MINWIDTH : CGFloat = 30
let MESSAGECELL_IMAGE_MINHEIGHT : CGFloat = 30

//这里是有问题的,需要给的是图片适配器,这里给的是text适配器
let MESSAGECELL_IMAGE_MAXWIDTH = MsgCellLayoutAdapterConfig.share.msgContentMaxWidth()

let MESSAGECELL_IMAGE_MAXHEIGHT : CGFloat = 800

//图片宽高
func messageCell_imageSizeToFitSize(imageSize : CGSize) -> CGSize {
    /*
     1
     宽比高的比例最小值为 minwidth : maxheight
     如果超过上述底线,说明图片过高,则直接按照宽度为最小值,高度为最大值,裁切图片展示.
     ===至此已完成图片比例控制以及展示
     2
     宽比高的比例最大值为 maxwidth : minheight
     如果超过上述上线,说明图片过宽,则直接按照宽度为最大值,高度为最小值,裁切图片展示.
     ===至此已完成图片比例控制以及展示
     3
     当图片比例在范围内时.
     (在范围内时说明,任何一方过大,按照比例缩小至下限或扩大至上限,依然不会超出比例范围)
     判断
     宽是否过大
     高是否过大
     若都过大,按照 图片尺寸比最大尺寸比值最大的一个边为准
     若上述成立,则
     按照过大一方,比例缩小至上限宽度
     ===至此已完成图片比例控制以及展示
     4
     判断
     宽是否过小
     高是否过小
     若都过小,按照 图片尺寸比最小尺寸比值最小的一个边为准
     若上述成立,则
     按照过小一方,比例扩大至下限宽度
     ===至此已完成图片比例控制以及展示
     5
     当以上都不成立时
     按照正常尺寸展示
     ===至此已完成图片比例控制以及展示
     */

    //bug补充
    if (imageSize.width == 0 || imageSize.height == 0) {
        return CGSizeMake(MESSAGECELL_IMAGE_MINWIDTH, MESSAGECELL_IMAGE_MINHEIGHT);
    }
    
    //第一步,判断比例
    let currentSizeW = imageSize.width
    let currentSizeH = imageSize.height
    let imageAspectRatio = currentSizeW / currentSizeH;
    let minAspectRatio = MESSAGECELL_IMAGE_MINWIDTH / MESSAGECELL_IMAGE_MAXHEIGHT;
    let maxAspectRatio = MESSAGECELL_IMAGE_MAXWIDTH / MESSAGECELL_IMAGE_MINHEIGHT;
    if (imageAspectRatio < minAspectRatio || imageAspectRatio > maxAspectRatio) {
        //在比例超限的前提下 宽度过宽 或 高度过高
        var fitWidth : CGFloat = 0;
        var fitHeight : CGFloat = 0;
        if (currentSizeW < MESSAGECELL_IMAGE_MINWIDTH) {
            fitWidth = MESSAGECELL_IMAGE_MINWIDTH;
        }else if (currentSizeW > MESSAGECELL_IMAGE_MAXWIDTH) {
            fitWidth = MESSAGECELL_IMAGE_MAXWIDTH;
        }else{
            fitWidth = currentSizeW;
        }
        if (currentSizeH < MESSAGECELL_IMAGE_MINHEIGHT) {
            fitHeight = MESSAGECELL_IMAGE_MINHEIGHT;
        }else if (currentSizeH > MESSAGECELL_IMAGE_MAXHEIGHT) {
            fitHeight = MESSAGECELL_IMAGE_MAXHEIGHT;
        }else{
            fitHeight = currentSizeH;
        }
        return CGSize(width: fitWidth,height: fitHeight)
    }
    
    //第二步,在范围内.判断是否小于最小值.
    //宽度
    if (currentSizeW < MESSAGECELL_IMAGE_MINWIDTH) {
        let fitHeight =  currentSizeH * MESSAGECELL_IMAGE_MINWIDTH / currentSizeW;
        return CGSize(width: CGFloat(MESSAGECELL_IMAGE_MINWIDTH), height: CGFloat(floorf(Float(fitHeight))));
    }
    //高度
    if (currentSizeH < MESSAGECELL_IMAGE_MINHEIGHT) {
        let fitWidth =  currentSizeW * MESSAGECELL_IMAGE_MINHEIGHT / currentSizeH;
        return CGSize(width: CGFloat(floorf(Float(fitWidth))),height: CGFloat(MESSAGECELL_IMAGE_MINHEIGHT))
    }
    
    //第三步,判断是否大于最大值.
    //宽度
    if (currentSizeW > MESSAGECELL_IMAGE_MAXWIDTH) {
        let fitHeight = currentSizeH * MESSAGECELL_IMAGE_MAXWIDTH / currentSizeW;
        return CGSize(width: CGFloat(MESSAGECELL_IMAGE_MAXWIDTH), height: CGFloat(floorf(Float(fitHeight))))
    }
    //高度
    if (currentSizeH > MESSAGECELL_IMAGE_MAXHEIGHT) {
        let fitWidth = currentSizeW * MESSAGECELL_IMAGE_MAXHEIGHT / currentSizeH;
        return CGSize(width: CGFloat(floorf(Float(fitWidth))), height: CGFloat(MESSAGECELL_IMAGE_MAXHEIGHT))
    }
    return imageSize;

}


func messageCell_videoCoverFitSizeFromCoverSize(size : CGSize) -> CGSize {
//    if (size.width > size.height) {
//        return CGSizeMake(EMsgCellLayoutAdapterConfigs.shared.msgContentMaxWidth, ceilf(EMsgCellLayoutAdapterConfigs.shared.msgContentMaxWidth * 9 / 16));
//    }
//    return CGSizeMake(ceilf(EMsgCellLayoutAdapterConfigs.shared.msgContentMaxWidth * 9 / 16),EMsgCellLayoutAdapterConfigs.shared.msgContentMaxWidth);
    return CGSize.zero
}

func convertToEdgeInsets_direction(direction : EMMessageDirection,top: CGFloat,bottom: CGFloat,fromSide: CGFloat,toSide: CGFloat) -> UIEdgeInsets{
    switch (direction) {
    case .send:
        return UIEdgeInsets(top: top, left: toSide, bottom: bottom, right: fromSide)
    case .receive:
        return UIEdgeInsets(top: top, left: fromSide, bottom: bottom, right: toSide)
    default:
        return UIEdgeInsets.zero;
    }
}

